package com.study.springmvc.web.module.controller;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public abstract class BaseController implements Controller {
    protected ModelAndView mav    = new ModelAndView();

    protected String       action;

    protected Log          logger = LogFactory.getLog(this.getClass());

    private String         viewPage;

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
        try {
            action = request.getParameter("action");
            Method method = this.getClass().getMethod(action,
                new Class[] { HttpServletRequest.class, HttpServletResponse.class });
            viewPage = (String) method.invoke(request, response);

        } catch (Exception e) {
            if (logger.isInfoEnabled()) {
                logger.info("action run error,please check action properties!");
            }
            viewPage = "/error";
        }
        mav.setViewName(viewPage);
        return mav;
    }
}
